package com.jy.shenghuilogistics.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jy.shenghuilogistics.bean.Cargo;
import com.jy.shenghuilogistics.bean.Document;
import com.jy.shenghuilogistics.bean.Logininfo;
import com.jy.shenghuilogistics.bean.Sitetb;
import com.jy.shenghuilogistics.dao.CargoMapper;
import com.jy.shenghuilogistics.dao.DocumentMapper;
import com.jy.shenghuilogistics.dao.SiteMapper;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service()
public class DocumentService {
    @Resource
    DocumentMapper documentMapper;
    @Resource
    CargoMapper cargoMapper;
    @Resource
    SiteMapper siteMapper;

    //运送中订单数
    public int inTransitOrderNum(int empId){
        return documentMapper.inTransitOrderNum(empId);
    }
    //总订单数
    public int allOrderNum(int empId){
        return documentMapper.allOrderNum(empId);
    }
    //已完成订单数
    public int offTheStocksOrderNum(int empId){
        return documentMapper.offTheStocksOrderNum(empId);
    }

    /*通过条件查询数据*/
    public List<Document> selAllDocByCondition(Document doc){
        return documentMapper.selAllDocByCondition(doc);
    }
    /*查询近大于30天数据*/
    public List<Document> selDocumentGT30(int id){
        return documentMapper.selDocumentGT30(id);
    }
    /*查询近30天数据*/
    public List<Document> selDocumentD30(int id){
        return documentMapper.selDocumentD30(id);
    }
    /**
     * 对应站点最近7日单量
     * @param siteName
     * @return
     */
    public List<Integer> selSevenDayDocBySite(String siteName) {
        return documentMapper.selSevenDayDocBySite(siteName);
    }
    /**
     * 修改运单
     * @param d
     * @return
     */
    @Transactional
    public String updDoc(Document d) {
        Logininfo l = new Logininfo();
        l.setDocId(d.getDocId());
        l.setDisTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        if("待配送".equals(d.getState()) || "待自提".equals(d.getState())){
            l.setNewSite(d.getDocEnd());
            l.setState("已收货");
            documentMapper.insLoginInfo(l);
        }else {
            Document doc= getDocumentById(d.getDocId());
            if("已收货".equals(doc.getLogInfos().get(0).getState())) {
                l.setNewSite(doc.getLogInfos().get(0).getNewSite());
                l.setEndSite(d.getDocEnd());
                l.setState("已收货");
                l.setEndSite(null);
                documentMapper.insLoginInfo(l);
            }
        }
        d.setDocStart(null);
        return documentMapper.updDoc(d).toString();
    }
    /**
     * 模糊查询所有运单
     * @param d
     * @return
     */
    public List<Document> getAllDoc(Document d) {
        return documentMapper.selAllDoc(d);
    }
     /**
     * 修改运单
     * @param d
     * @return
     */
    public String updateDocu(Document d) {
        if("已签收".equals(d.getState())) {
            Logininfo l = new Logininfo();
            l.setDocId(d.getDocId());
            l.setNewSite(d.getDocEnd());
            l.setState(d.getState());
            l.setDisTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            documentMapper.insLoginInfo(l);
        }
        return documentMapper.updDoc(d).toString();
    }
    /**
     * 批量运单接收
     * @param nowSite
     * @param documents
     * @return
     */
    @Transactional
    public String listDocReceive(String nowSite,List<Document> documents) {
        for (Document d : documents) {
            addLoginInfo(d.getDocId(),nowSite,d.getDocEnd(),"已收货");
            if(nowSite.equals(d.getDocEnd())) {
                if("自提".equals(d.getDelivery())) {
                    d.setState("待自提");
                }else {
                    d.setState("待配送");
                }
                documentMapper.updDoc(d);
            }
        }
        return "success";
    }
    /**
     * 获取对应站点的所有运单
     * @param siteName
     * @return
     */
    public List<Document> getAllDocBySiteName(String siteName) {
        return documentMapper.selAllDocBySiteId(siteName);
    }
    /**
        获取对应id的订单
     */
    public Document getDocumentById(int id) {
        return documentMapper.selDocumentById(id);
    }

    /**
     * 添加运单
     * @param d
     * @return
     * @throws Exception
     */
    @Transactional
    public String addDocument(Document d) throws Exception {
        String result = "success";
        if(documentMapper.insDoc(d) <= 0) {
            result = "运单信息异常";
        }
        for (Cargo cargo : d.getCargos()) {
            cargo.setDocId(d.getDocId());
            if(cargoMapper.insCargo(cargo) <= 0) {
                result = "货物信息异常";
            }
        }
        if(result == "success") {
            addLoginInfo(d.getDocId(),d.getDocStart(),d.getDocEnd(),"已收货");
        }
        return result;
    }

    /**
     * 添加送货信息
     * @param DocId
     * @param nowSite
     * @param state
     */
    public void addLoginInfo(Integer DocId,String nowSite,String state) {
        Logininfo l = new Logininfo();
        l.setDocId(DocId);
        l.setNewSite(nowSite);
        l.setState(state);
        l.setDisTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        documentMapper.insLoginInfo(l);
    }
    /**
     * 添加物流信息
     */
    public void addLoginInfo(Integer DocId,String nowSite,String endSite,String state) {
        Sitetb nowS = siteMapper.selSiteByName(nowSite);
        Sitetb endS = siteMapper.selSiteByName(endSite);
        Logininfo l = new Logininfo();
        l.setDocId(DocId);
        l.setNewSite(nowSite);
        l.setState(state);
        l.setDisTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //如果当前站点等于终点站
        if(nowS.getSiteName().equals(endS.getSiteName())) {
            documentMapper.insLoginInfo(l);
            return;
        }
        //如果当前站点所在市等于终点站所在市(但是不在一个区)
        if(nowS.getAreatb().getCityId() == endS.getAreatb().getCityId()) {
            if("区".equals(nowS.getSiteGrade())) {
                l.setEndSite(getCitySite(nowS.getAreatb().getCityId()).getSiteName());
                documentMapper.insLoginInfo(l);
                return;
            }
            if("市".equals(nowS.getSiteGrade()) || "省".equals(nowS.getSiteGrade())) {
                l.setEndSite(endSite);
                documentMapper.insLoginInfo(l);
                return;
            }
        }
        //如果当前站点所在省等于终点站所在省(但是不在一个市)
        if(nowS.getAreatb().getCitytb().getProId() == endS.getAreatb().getCitytb().getProId()) {
            if("区".equals(nowS.getSiteGrade())) {
                l.setEndSite(getCitySite(nowS.getAreatb().getCityId()).getSiteName());
                documentMapper.insLoginInfo(l);
                return;
            }
            if("市".equals(nowS.getSiteGrade())) {
                l.setEndSite(getProSite(nowS.getAreatb().getCitytb().getProId()).getSiteName());
                documentMapper.insLoginInfo(l);
                return;
            }
            if("省".equals(nowS.getSiteGrade())) {
                l.setEndSite(getCitySite(endS.getAreatb().getCityId()).getSiteName());
                documentMapper.insLoginInfo(l);
                return;
            }
        }else{
            //如果当前站点所在省不等于终点站所在省
            if("区".equals(nowS.getSiteGrade())) {
                l.setEndSite(getCitySite(nowS.getAreatb().getCityId()).getSiteName());
                documentMapper.insLoginInfo(l);
                return;
            }
            if("市".equals(nowS.getSiteGrade())) {
                l.setEndSite(getProSite(nowS.getAreatb().getCitytb().getProId()).getSiteName());
                documentMapper.insLoginInfo(l);
                return;
            }
            if("省".equals(nowS.getSiteGrade())) {
                l.setEndSite(getProSite(endS.getAreatb().getCitytb().getProId()).getSiteName());
                documentMapper.insLoginInfo(l);
                return;
            }
        }
    }
    //获取城市对应市级总站点
    public Sitetb getCitySite(int cityId) {
        for(Sitetb s : siteMapper.selProSite()) {
            if(s.getAreatb().getCityId() == cityId) {
                return s;
            }
        }
        for(Sitetb s : siteMapper.selCitySite()) {
            if(s.getAreatb().getCityId() == cityId) {
                return s;
            }
        }
        return new Sitetb();
    }

    //获取省对应省级总站点
    public Sitetb getProSite(int proID) {
        for(Sitetb s : siteMapper.selProSite()) {
            if(s.getAreatb().getCitytb().getProId() == proID) {
                return s;
            }
        }
        return new Sitetb();
    }
}
